package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.user.UserDto;
import com.lnking.lnkingplace.model.entity.FileList;
import com.lnking.lnkingplace.service.IUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author sn
 * @date 2018/11/1
 * @description 文件上传
 */
@Api(value = "文件上传", tags = "文件上传")
@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {

    @Autowired
    IUploadService uploadService;

    @Autowired
    UserUtil userUtil;

    @ApiOperation("文件上传")
    @PostMapping(value = "/file")
    public ReturnResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        UserDto user = userUtil.getUser();
        FileList fileEntity = uploadService.uploadFile(file, user.getName());
        return ReturnResult.success(fileEntity);
    }

    @ApiOperation("文件上传(不用登录)")
    @PostMapping(value = "/fileNoAuth")
    public ReturnResult uploadFileNoAuth(@RequestParam("file") MultipartFile file) throws Exception {
        FileList fileEntity = uploadService.uploadFile(file, "-");
        return ReturnResult.success(fileEntity);
    }
}
